package StringPackage;

import org.junit.jupiter.api.Test;

/**
 * @author Lzm
 * @version 1.0
 */
public class reverseWords_ {
  @Test
  public void takeStringTest() {
    String s = "  hello world  ";
    System.out.println(takeString(s));
  }
  public String takeString(String s){
     int slow = 0;
    char[] chars = s.toCharArray();
    for (int fast = 0;fast < chars.length ; fast++){
      if (chars[fast] != ' '){
        if (slow != 0 && chars[fast - 1] == ' '){
          chars[slow++] = ' ';
        }
          chars[slow++] = chars[fast];
      }
    }
    return new String(chars,0,slow);
  }

  public static String takeString1(String s){
    int slow = 0;
    char[] chars = s.toCharArray();
    for (int fast = 0 ; fast < chars.length ; fast ++){
      if (chars[fast] != ' '){
        if (slow != 0 && chars[fast - 1] == ' '){
          chars[slow++] = ' ';
        }
        chars[slow++] = chars[fast];
      }
    }
    return new String(chars,0,slow);
  }
}
